use std::sync::{Arc, Mutex};
use crate::context::Context;
use crate::message::Message;
use crate::actors::Actor;
use crate::mailbox::{Mailbox, MailboxType, QueueType};

// 定义 Actor 系统
pub struct ActorSystem {
    actors: Arc<Mutex<Vec<Mailbox>>>,
}

impl ActorSystem {
    pub fn new() -> Self {
        ActorSystem {
            actors: Arc::new(Mutex::new(Vec::new())),
        }
    }

    pub fn spawn(&self, actor: Arc<Mutex<dyn Actor + Send>>, mailbox_type: MailboxType, queue_type: QueueType) -> usize {
        let mailbox = Mailbox::new(actor, mailbox_type, queue_type);
        let mut actors = self.actors.lock().unwrap();
        actors.push(mailbox);
        actors.len() - 1
    }

    pub async fn send(&self, actor_id: usize, msg: Box<dyn Message>) {
        let actors = self.actors.lock().unwrap();
        if let Some(mailbox) = actors.get(actor_id) {
            mailbox.send(msg).await;
        }
    }
}